package com.tj.jinzao.presenter;


import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IFallowView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.FollowFan;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 * Created by wjy 2018/12/10.
 */
public class FollowFanPresenter extends BasePresenter<IFallowView<List<FollowFan>>> {

    public FollowFanPresenter(IFallowView<List<FollowFan>> view) {
        super(view);
    }

    public void getUserFans(int page) {
        long id = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getUserFans(page, id), new SubscriberCallBack<List<FollowFan>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<FollowFan> users) {
                if (mView != null) mView.onRequestSuccess(users);
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void getUserFollow(int page) {
        long id = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getUserFollow(page, id), new SubscriberCallBack<List<FollowFan>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<FollowFan> users) {
                if (mView != null) mView.onRequestSuccess(users);
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void updateInfo(Map<String, Object> map) {
        long id = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.updateUserInfo(id, map), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                User currentUser = UserManager.getInstance().getUser();
                user.setAuthentication(currentUser.getAuthentication());
                UserManager.saveUserData(user);
                if (mView != null) mView.onRequestSuccess(null);
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void followUser(long followId) {
        long id = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.followUser(id, followId), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String success) {
                List<FollowFan> list = new ArrayList();
                FollowFan fan = new FollowFan();
                fan.setId(0);
                fan.setFollowUid(success==null ? 1 : 0);
                list.add(fan);
                if (mView != null) mView.onAttentSuccess(list);
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onAttentFailed(response.getMessage());
            }
        });
    }
}